package frame.options.simulation.results.physicalNetworks.numerical;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import model.components.networks.network.PhysicalNetwork;
import frame.options.simulation.results.ResultsFrame;

@SuppressWarnings("serial")
public class PhysicalNetworkTablesPanel extends JPanel implements ChangeListener
{
	private List< PhysicalNetwork > physicalNetworks;
	public List< PhysicalNetwork > getPhysicalNetworks(){ return physicalNetworks; }
	public void setPhysicalNetworks( List< PhysicalNetwork > physicalNetworks ){ this.physicalNetworks = physicalNetworks; }
	
	private ResultsFrame resultsFrame;
	public ResultsFrame getResultsFrame(){ return resultsFrame; }
	public void setResultsFrame( ResultsFrame resultsFrame ){ this.resultsFrame = resultsFrame; }
	
	private List< JScrollPane > physicalNetworkTablesScrollPanes;
	public List< JScrollPane > getPhysicalNetworkTablesScrollPanes(){ return physicalNetworkTablesScrollPanes; }
	public void setPhysicalNetworkTablesScrollPanes( List< JScrollPane > physicalNetworkTablesScrollPanes ){ this.physicalNetworkTablesScrollPanes = physicalNetworkTablesScrollPanes; }
	
	private JLabel label;
	public JLabel getLabel(){ return label; }
	public void setLabel( JLabel label ){ this.label = label; }
	
	private JSlider slider;
	public JSlider getSlider(){ return slider; }
	public void setSlider( JSlider slider ){ this.slider = slider; }
	
	private int sliderPreviousValue;
	public int getSliderPreviousValue(){ return sliderPreviousValue; }
	public void setSliderPreviousValue( int sliderPreviousValue ){ this.sliderPreviousValue = sliderPreviousValue; }
	
	public PhysicalNetworkTablesPanel( List< PhysicalNetwork > physicalNetworks, ResultsFrame resultsFrame )
	{
		this.setPhysicalNetworks( physicalNetworks );
		this.setResultsFrame( resultsFrame );
		
		this.setPhysicalNetworkTablesScrollPanes( this.physicalNetworkTablesScrollPanes( physicalNetworks ) );
		
		this.setLabel( new JLabel( physicalNetworks.get( 0 ).getName(), null, SwingConstants.CENTER ) );
		this.setSlider( this.slider( physicalNetworks.size() ) );
		this.setLayout( new BorderLayout() );
		this.add( this.getLabel(), BorderLayout.NORTH );
		this.add( this.getPhysicalNetworkTablesScrollPanes().get( 0 ), BorderLayout.CENTER );
		this.add( this.getSlider(), BorderLayout.SOUTH );
	}
	
	private List< JScrollPane > physicalNetworkTablesScrollPanes( List< PhysicalNetwork > physicalNetworks )
	{
		List< JScrollPane > physicalNetworkTablesScrollPanes = new ArrayList< JScrollPane >();
		
		for( PhysicalNetwork physicalNetwork: physicalNetworks )
			physicalNetworkTablesScrollPanes.add( new JScrollPane( new PhysicalNetworkTable( physicalNetwork ) ) );
		
		return physicalNetworkTablesScrollPanes;
	}
	
	private JSlider slider( int physicalNetworksNumber )
	{
		JSlider slider = new JSlider();
		slider.setPaintTicks( true );
		slider.setSnapToTicks( true );
		slider.setPaintTrack( true );
		slider.setPaintLabels( true );
		slider.setMinimum( 0 );
		slider.setMaximum( physicalNetworksNumber - 1 );
		slider.setMinorTickSpacing( 1 );
		slider.setMajorTickSpacing(	1 );
		slider.setValue( 0 );
		slider.addChangeListener( this );
		
		return slider;
	}
	
	@Override
	public void stateChanged( ChangeEvent changeEvent )
	{
		this.putValueSliders( this.getSlider().getValue() );
	}
	
	public void putValueSliders( int valueSlider )
	{
		this.setSliderValue( valueSlider );
		
		int selectedIndex = this.getResultsFrame().getPhysicalNetworksTablesPanelsTabbedPane().getSelectedIndex();
		this.getResultsFrame().getPhysicalNetworksVisualizationViewersPanelsTabbedPane().setSelectedIndex( selectedIndex );
		this.getResultsFrame().getPhysicalNetworksVisualizationViewersPanels().get( selectedIndex ).setSliderValue( valueSlider );
	}
	
	public void setSliderValue( int valueSlider )
	{
		PhysicalNetwork physicalNetwork = this.getPhysicalNetworks().get( valueSlider );
		
		if( physicalNetwork.getTimeStamp() == -1 )
			this.getLabel().setText( physicalNetwork.getName() );
		else
			this.getLabel().setText( "Time: " + physicalNetwork.getTimeStamp() );
		
		this.remove( this.getPhysicalNetworkTablesScrollPanes().get( this.getSliderPreviousValue() ) );
		this.setSliderPreviousValue( valueSlider );
		this.add( this.getPhysicalNetworkTablesScrollPanes().get( valueSlider ), BorderLayout.CENTER );
		this.getSlider().setValue( valueSlider );
		this.repaint();
	}
	
}
